package com.lyy.controller;

import com.lyy.entity.param.CartInsertParam;
import com.lyy.result.Result;
import com.lyy.result.ResultCode;
import com.lyy.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Liangyaoyun
 */
@Tag(name = "CartController", description = "购物车接口模块")
@RestController
@RequestMapping("/api/v1/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "添加购物车记录")
    @PostMapping("/insert")
    public Result insert(@RequestBody @Validated CartInsertParam cartInsertParam, HttpSession httpSession) {
        Integer userId = (Integer) httpSession.getAttribute("userId");
        cartInsertParam.setUserId(userId);
        cartService.insert(cartInsertParam);
        return new Result(ResultCode.SUCCESS);
    }

    @Operation(summary = "查询购物车记录")
    @GetMapping("/list")
    public Result list(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        List<CartInsertParam> result = cartService.selectByUserId(userId);
        return new Result(result);
    }

    @Operation(summary = "批量删除购物车记录")
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestParam Integer[] videoIds, HttpSession httpSession) {
        Integer userId = (Integer) httpSession.getAttribute("userId");
        cartService.batchDelete(userId, videoIds);
        return new Result(ResultCode.SUCCESS);
    }

    @Operation(summary = "清空购物车记录")
    @PostMapping("/clear")
    public Result clear(HttpSession httpSession) {
        Integer userId = (Integer) httpSession.getAttribute("userId");
        cartService.deleteByUserId(userId);
        return new Result(ResultCode.SUCCESS);
    }

}